<?php

namespace App\Models;

class Mails extends Base
{
    protected $table = 'mails';

    const TYPE_1 = 1;
    const TYPE_2 = 2;

    const TYPE_TEXT = [
        self::TYPE_1 => '<span class="el-tag">通知</span>',
        self::TYPE_2 => '<span class="el-tag el-tag--danger">系统</span>',
    ];

    const STATUS_1 = 1;
    const STATUS_2 = 2;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag el-tag--danger">未读</span>',
        self::STATUS_2 => '<span class="el-tag">已读</span>',
    ];

    protected $fillable = [
        'id','title','content','type','created_at','updated_at','message_id','user_id','status'
    ];

    public function user(){
        return $this->belongsTo('App\Models\Users', 'user_id', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
            })->where('status','<>',-1)
            ->with(['user' => function ($query) {
                $query->select(['id','username'])->get();
            }])->orderBy('created_at', 'desc')
            ->paginate($limit);
        $this->stateToText($lists,[
            'type' => self::TYPE_TEXT,
            'status' => self::STATUS_TEXT,
        ]);
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = null, $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        if(isset($page)){
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                })->where('status','<>',-1)
                ->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else{
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                })->where('status','<>',-1)
                ->orderBy($sort, $orderby)->get();
        }
        return $lists;
    }

    /**
     * 根据id查询一条信息
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function details($params, $fields = ['*']){
    	$detail = $this->select($fields)->where($params)->where('status','<>',-1)->first();
    	if($params['user_id']>0){
	    	$detail['pre_id'] = $this->where('user_id',$params['user_id'])->where('status','<>',-1)->where('id', '<', $detail['id'])->max('id');
	    	if($detail['pre_id']>0){
	    		$detail['pre_title'] = $this->where('id', $detail['pre_id'])->value('title');
	    	}else{
	    		$detail['pre_title'] = "没有了";
	    	}
	    	
	    	$detail['next_id'] = $this->where('user_id',$params['user_id'])->where('status','<>',-1)->where('id', '>', $detail['id'])->min('id');
	    	if($detail['next_id']>0){
	    		$detail['next_title'] = $this->where('id',$detail['next_id'])->value('title');
	    	}else{
	    		$detail['next_title'] = "没有了";
	    	}
    	}
        return $detail;
    }

    /**
     * 发送站内消息
     * @param string $title 标题
     * @param string $content 内容
     * @param int $user_id 查看用户id
     * @return mixed
     */
    protected function sendMails($title = '', $content = '', $user_id = '', $type = 1){
        $arr = array(
            'title' => $title,
            'content' => $content,
            'user_id' => $user_id,
            'type' => $type,
        );
        Mails::create($arr);
    }

}
